package com.stockz.core.manager;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.springframework.stereotype.Component;

@Component
public class ImportExportManager {
	
	public void exportData(String path, Object target) throws FileNotFoundException{
		XMLEncoder xmlEncoder = null;
		try{
			xmlEncoder = new XMLEncoder(new FileOutputStream(path));
			xmlEncoder.writeObject(target);
		}finally{
			if(xmlEncoder != null){
				xmlEncoder.close();
			}
		}
	}
	
	public Object importData(String path) throws FileNotFoundException{
		XMLDecoder xmlDecoder = null;
		Object output = null;
		try{
			xmlDecoder = new XMLDecoder(new FileInputStream(path));
			output = xmlDecoder.readObject(); 
		}finally{
			if(xmlDecoder != null){
				xmlDecoder.close();
			}
		}
		return output;
	}
	
}
